﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CL_SQL.DAL
{
    public class LoginDAL
    {
        LinqDBDataContext lqDB = new LinqDBDataContext("Data Source=.;Initial Catalog=StudentLoan;Persist Security Info=True;User ID=sa;Password=sa");
        internal UserLogin LoginCheck(string UserAccount, string pwd)
        {
            try
            {
                var temp = (from g in lqDB.UserLogin where g.UserAccount == UserAccount && g.UserPassword == pwd select g).ToList()[0]; 
                if (temp != null) {
                    UserLogin userLogin = new UserLogin();
                    userLogin.UserId = temp.UserId;
                    userLogin.UserAccount = temp.UserAccount;
                    userLogin.UserName = temp.UserName;
                    userLogin.UserPassword = temp.UserPassword;
                    userLogin.LastModifyDate = temp.LastModifyDate;
                    userLogin.LastModifyUser = temp.LastModifyUser;
                    return userLogin;
                }
                else
                {
                    return null;
                }   
            }catch(Exception ex)
            {
                return null;
                throw ex;
            }
           
        }
        internal Boolean ChangePwd(string U, string O, string N)
        {
            try {
                var temp= from g in lqDB.UserLogin where g.UserAccount==U && g.UserPassword ==O select g;
                if(temp != null)
                {
                    var t = (from g in lqDB.UserLogin where g.UserAccount == U select g ).ToList()[0];
                    t.UserPassword = N;
                    lqDB.SubmitChanges();
                    return true;
                } else
                {
                    return false;
                }


            }catch(Exception ex)
            {
                return false;
                throw ex;
            }
        }
    }
}
